package crontab

import (
	global2 "longmen/server/config/global"
	"longmen/server/pkg/db/mysql/models/models_main"
	"strconv"
)

func PutUserBloomInit() {
	global2.Log.Infof("putUserBloom start")

	// 取出所有用户
	var users []models_main.User
	err := global2.GetX().Select("mobile,id").Model(&models_main.User{}).Scan(&users).Error
	if err != nil {
		global2.Log.Errorf("get user error:%s", err.Error())
		return
	}

	global2.Log.Debug("putUserBloom users num :", len(users))

	//var accounts []string
	var mobiles []string
	var ids []string
	for _, v := range users {
		//accounts = append(accounts, v.Account)
		mobiles = append(mobiles, v.Mobile)
		ids = append(ids, strconv.Itoa(v.Id))
	}

	// 加入布隆过滤器
	_, err = global2.BloomBfAddMulti(global2.BLOOM_ID, ids)
	if err != nil {
		global2.Log.Errorf("add ID bloom error[%v]: ", err)
		return
	}
	adds, err := global2.BloomBfAddMulti(global2.BLOOM_MOBILE, mobiles)
	if err != nil {
		global2.Log.Errorf("add mobile bloom error:%s", err)
		return
	} else {
		num := putBloomNum(adds)
		global2.Log.Infof("add mobile bloom:%d", num)
	}

	global2.Log.Infof("putUserBloom end")
}

func putBloomNum(adds []int64) []int64 {
	var t []int64
	for _, add := range adds {
		if add == 1 {
			t = append(t, add)
		}
	}
	return t
}

func PutUserBloom() {
	global2.Log.Infof("putUserBloom start")

	// 取出所有用户
	var users []models_main.User

	err := global2.GetX().Model(&models_main.User{}).
		Select("id,mobile").
		Where("regist_time >  DATE_ADD(NOW(),INTERVAL - 30 SECOND)").
		Scan(&users).Error
	if err != nil {
		global2.Log.Errorf("get user error:%s", err.Error())
		return
	}
	global2.Log.Debug("putUserBloom users num :", len(users))

	// 加入布隆过滤器
	var mobiles []string
	var ids []string
	for _, v := range users {
		ids = append(ids, strconv.Itoa(v.Id))
		mobiles = append(mobiles, v.Mobile)
	}

	if len(ids) > 0 {
		_, err = global2.BloomBfAddMulti(global2.BLOOM_ID, ids)
		if err != nil {
			global2.Log.Errorf("add ID bloom error[%v]: ", err)
			return
		}
	}

	if len(mobiles) > 0 {
		adds, err := global2.BloomBfAddMulti(global2.BLOOM_MOBILE, mobiles)
		if err != nil {
			global2.Log.Errorf("add mobile bloom error:%s", err.Error())
			return
		} else {
			num := putBloomNum(adds)
			global2.Log.Infof("add mobile bloom:%d", num)
		}
	}

	global2.Log.Infof("putUserBloom end")
}

func PutAnchorBloomInit() {
	putAnchorBloom("all")
}

func PutAnchorBloomIncrease() {
	putAnchorBloom("increase")
}

func putAnchorBloom(putType string) {
	global2.Log.Infof("putAnchorBloom start")

	// 取出所有用户
	var users []models_main.User
	var err error
	if putType == "all" {
		err = global2.GetX().Model(&models_main.User{}).
			Select("id,mobile").Where("is_anchor = 1").
			Scan(&users).Error
	} else {
		err = global2.GetX().Model(&models_main.User{}).
			Select("id,mobile").
			Where("is_anchor = 1 and auth_time >  DATE_ADD(NOW(),INTERVAL - 30 SECOND)").
			Scan(&users).Error
	}

	if err != nil {
		global2.Log.Errorf("get user error:%s", err.Error())
		return
	}
	global2.Log.Debug("putUserBloom anchor num :", len(users))

	// 加入布隆过滤器
	var mobiles []string
	var ids []string
	for _, v := range users {
		ids = append(ids, strconv.Itoa(v.Id))
		mobiles = append(mobiles, v.Mobile)
	}

	if len(ids) > 0 {
		_, err = global2.BloomBfAddMulti(global2.BLOOM_ANCHOR, ids)
		if err != nil {
			global2.Log.Errorf("add ID bloom error[%v]: ", err)
			return
		}
	}
}
